package com.ap.core.validation.message.rules;

import com.ap.core.cxf.jaxb.common.DeliveryAddressType;
import com.ap.core.cxf.jaxb.common.DeliveryInfoType;
import com.ap.core.cxf.jaxb.common.DeliveryType;
import com.ap.core.cxf.jaxb.managertp.SubmitRTPMessageType;
import com.ap.core.cxf.jaxb.rtp.TransactionCheckOutType;
import com.ap.core.message.managertp.info.SubmitRTPMessageInfo;
import com.ap.core.validation.message.info.MessageValidationInfo;
import com.ap.core.validation.message.info.OriginalInfo;
import com.ap.core.validation.rules.AbstractValidationRule;

/**
 * User: Somaraja.Surampudi The element <DeliveryAddress> is mandatory if the
 * <DeliveryType>=Delivery to address or Store Collection, and the
 * <TxnChkOut>=NRMLCHK
 * ValidDeliveryAddressRule
 */
public class IF0007Rule	extends
						AbstractValidationRule<MessageValidationInfo<SubmitRTPMessageInfo, OriginalInfo>> {

	@Override
	public boolean validate(
			MessageValidationInfo<SubmitRTPMessageInfo, OriginalInfo> infoObject) {
		boolean pass = true;
        final SubmitRTPMessageInfo submitRTPMessageInfo = infoObject
				.getBoundObject();

		final SubmitRTPMessageType messageType = submitRTPMessageInfo.getMsgHdr()
				.getMsgType();
		final TransactionCheckOutType transactionCheckOutType = submitRTPMessageInfo
				.getRTPSummary().getTxCheckout();
		final DeliveryInfoType delivery = submitRTPMessageInfo.getDelivery();

		if (null != delivery) {
            DeliveryAddressType deliveryAddress = delivery
					.getDeliveryAddress();
			DeliveryType deliveryType = delivery.getDeliveryType();

			if ((SubmitRTPMessageType.SUBMITRTPFORMOB == messageType || SubmitRTPMessageType.SUBMITRTPFORBRN == messageType)) {
                if((TransactionCheckOutType.NRMLCHK == transactionCheckOutType)
                        && (DeliveryType.DELTAD == deliveryType || DeliveryType.COLLST == deliveryType)){
                     pass = null != deliveryAddress;
                } else{ if(TransactionCheckOutType.QKPACHK == transactionCheckOutType){
                    pass = null == deliveryAddress;
                 }
                }
			}
		}
		return pass;
	}

}